package com.chwn.curiousdiary.ui.user.presenter;

import com.chwn.curiousdiary.DiaryApplication;
import com.chwn.curiousdiary.api.IUserApi;
import com.chwn.curiousdiary.api.NetWork;
import com.chwn.curiousdiary.bean.MessageBean;
import com.chwn.curiousdiary.gloable.Constant;
import com.chwn.curiousdiary.gloable.User;
import com.chwn.curiousdiary.ui.user.contract.MessageContract;
import com.chwn.harulib.mvp.rx.HttpResult;
import com.chwn.harulib.mvp.rx.HttpResultException;
import com.chwn.harulib.mvp.rx.RxSchedulers;

import java.util.List;

import rx.Observer;

/**
 * Created by Myshikin on 2017/3/24.
 */

public class MessagePresenter extends MessageContract.Presenter {

    private Observer<HttpResult<List<MessageBean>>> messageObserver = new Observer<HttpResult<List<MessageBean>>>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            mView.onRequestError(e);
        }

        @Override
        public void onNext(HttpResult<List<MessageBean>> listHttpResult) {
            if (listHttpResult.getResult() == 1 && listHttpResult.getResdata() != null && listHttpResult.getResdata().size() > 0) {
                mView.showMessageList(listHttpResult.getResdata());
            } else {
                mView.onRequestError(new HttpResultException(2, "数据为空"));
            }
        }
    };

    @Override
    public void getMessageList(int page) {
        User user = DiaryApplication.mInstance.getmUser();
        String userToken = "";
        if (user != null) {
            userToken = user.getUserToken();
        }
        NetWork.getInstance()
                .getApiService(IUserApi.class)
                .getMessageList(page, userToken, Constant.OSTYPE, Constant.PAGE_COUNT)
                .compose(RxSchedulers.<HttpResult<List<MessageBean>>>io_main())
                .subscribe(messageObserver);
        //        test(page);
    }
}
